import shop from '../../api/shop'

const state = {
  productsList: []
}

const getters = {}

// actions —— 异步操作要放在 actions
const actions = {
  // 加载所有商品
  getAllProducts({ commit }) {
    // 从 shop API 加载所有商品，模拟异步
    shop.getProducts(products => {
      commit('setProducts', products)
    })
  }
}

const mutations = {
  setProducts(state, products) {
    state.productsList = products
  }
}

export default {
  namespaced: true,   // 命名空间
  state,
  getters,
  actions,
  mutations
}